module ApplicationHelper
  require 'string'
  require 'object'
  require 'mechanize'

  #Return a link for use in layout navigation.
  def nav_link(text, controller,action="index")
    return link_to_unless_current text, :controller => controller, :action => action
  end

  # Return true if some user is logged in, false otherwise.
  def logged_in?
    not session[:user_id].nil?
  end

  def text_field_for(form, field,title,
    size=HTML_TEXT_FIELD_SIZE,
    maxlength=DB_STRING_MAX_LENGTH)
    label = content_tag("label", title, :for => field)
    form_field = form.text_field field, :size => size, :maxlength => maxlength
    content_tag("div", "#{label} #{form_field}".html_safe, :class => "form_row")
  end

  # Return a link for use in site navigation.
  def nav_link(text, controller, action="index")
    link_to_unless_current text, :id => nil, :action => action, :controller => controller
  end

  def hace_tanto_tiempo (from_time, to_time = 0, include_seconds = false, language = nil)
    # Traducción de distance_of_time_in_words

    from_time = from_time.to_time if from_time.respond_to?(:to_time)
    to_time = to_time.to_time if to_time.respond_to?(:to_time)
    distance_in_minutes = (((to_time - from_time).abs)/60).round
    distance_in_seconds = ((to_time - from_time).abs).round

    case language || session[:language] || "es"
    when "ca" then say = {:less_seconds => 'menys de ? segons', :half_minute => 'mig minut', :less_minute => 'menys d\'un minut', :minutes => 'minuts', :one_minute => 'un minut', :hours => 'hores', :one_hour => 'una hora', :days => 'dies', :one_day => '1 dia', :months => 'mesos', :one_month => 'un mes', :years => 'anys', :one_year => 'un any'}
    when "es" then say = {:less_seconds => 'menos de ? segundos', :half_minute => 'medio minuto', :less_minute => 'menos de un minuto', :minutes => 'minutos', :one_minute => 'un minuto', :hours => 'horas', :one_hour => 'una hora', :days => 'd&iacute;as', :one_day => 'un d&iacute;a',:months => 'meses', :one_month => 'un mes', :years => 'a&ntilde;os', :one_year => 'un a&ntilde;o'}
    when "en" then  say = {:less_seconds => 'less than ? seconds', :half_minute => 'half a minute', :less_minute => 'lesss than a minute', :minutes => 'minutes', :one_minute => '1 minute', :hours => 'hours', :one_hour => 'about 1 hour', :days => 'days', :one_day => '1 day',:months => 'months', :one_month => 'about 1 month', :years => 'year', :one_year => 'about 1 year'}
    end

    case distance_in_minutes
    when 0..1
      return (distance_in_minutes==0) ? say[:less_minute] : say[:one_minute] unless include_seconds
      case distance_in_seconds
      when 0..4   then say[:less_seconds].sub(/\?/, '5')
      when 5..9   then say[:less_seconds].sub(/\?/, '10')
      when 10..19 then say[:less_seconds].sub(/\?/, '20')
      when 20..39 then say[:half_minute]
      when 40..59 then say[:less_minute]
      else             say[:one_minute]
      end
    when 2..44           then "#{distance_in_minutes} #{say[:minutes]}"
    when 45..89          then say[:one_hour]
    when 90..1439        then "#{(distance_in_minutes.to_f / 60.0).round} #{say[:hours]}"
    when 1440..2879      then say[:one_day]
    when 2880..43199     then "#{(distance_in_minutes / 1440).round} #{say[:days]}"
    when 43200..86399    then say[:one_month]
    when 86400..525959   then "#{(distance_in_minutes / 43200).round} #{say[:months]}"
    when 525960..1051919 then say[:one_year]
    else                      "#{(distance_in_minutes / 525960).round} #{say[:years]}"
    end
  end

  # Return true if results should be paginated.
  def paginated?
    @pages and @pages.length > 1
  end

  def pass_key_to_client_get
    agente= Mechanize.new { |agent|
      agent.user_agent_alias = 'Mac Safari'
      agent.redirection_limit=10000
    }
    return agente
  end
end
